from datetime import datetime
from ..services import inventory_service, api_client

def init_scheduled_jobs(app):
    """初始化定时任务"""
    
    @app.scheduler.scheduled_job('cron', hour=6)
    def daily_inventory_update():
        """每日库存更新任务"""
        with app.app_context():
            try:
                print(f'[{datetime.now()}] 开始执行库存更新...')
                # 调用API客户端获取数据
                raw_data = api_client.fetch_inventory_data()
                # 处理并保存数据
                inventory_service.process_raw_data(raw_data)
                print(f'[{datetime.now()}] 库存更新完成')
            except Exception as e:
                print(f'库存更新失败: {str(e)}')